package com.dw.controll;


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dw.dao.UserDao;
import com.dw.dao.impl.UserDaoImpl;
import com.dw.model.User;

@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public RegisterServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		this.doPost(request, response);	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String user = request.getParameter("username");
		String pass = request.getParameter("password");
		String register_code = request.getParameter("register_code");
		if(!"XXX".equals(register_code) ){
			System.out.println("register_code"+register_code);
			PrintWriter out = response.getWriter();//通过servlet的doget方法获取response对象，通过getWriter方法获取PrintWriter对象
			out.flush();//清空缓存
	         out.println("<script>");//输出script标签
	         out.println("alert(\"注册码错误，请联系管理员获取注册码！\");");
	         out.println("window.location=\"login.jsp\";");
	         out.println("</script>");//输出script结尾标签
			return;
		}
		
		System.out.println(user);
		System.out.println(pass);
		UserDao userdao = new UserDaoImpl();
		//参考mvc案例中的，检查管理员在数据库名字是否重复
		long count = userdao.getCountWithName(user);
		
		if(count > 0){
			//2.2.1 要求在 newcustomer.jsp 页面显示一个错误消息: 用户名 name 已经被占用, 请重新选择!
			//在 request 中放入一个属性 message: 用户名 name 已经被占用, 请重新选择!, 
			//在页面上通过 request.getAttribute("message") 的方式来显示
			request.setAttribute("message_register", "用户名" + user + "已经被占用, 请重新选择!");
			System.out.println(user+"已存在！");
			
			//2.2.2 newcustomer.jsp 的表单值可以回显. 
			//通过 value="<%= request.getParameter("name") == null ? "" : request.getParameter("name") %>"
			//来进行回显
			//2.2.3 结束方法: return 
			PrintWriter out = response.getWriter();//通过servlet的doget方法获取response对象，通过getWriter方法获取PrintWriter对象
			out.flush();//清空缓存
	         out.println("<script>");//输出script标签
	         out.println("alert(\"抱歉改用户名已被占用，请重新注册！\");");
	         out.println("window.location=\"login.jsp\";");
	         out.println("</script>");//输出script结尾标签
			return;
		}
		User user_new = new User(user, pass);
		boolean flag = userdao.addUser(user_new);
		if (flag) {
			System.out.println("添加成功!!");
			request.setAttribute("msg", "添加成功!!");
			PrintWriter out = response.getWriter();//通过servlet的doget方法获取response对象，通过getWriter方法获取PrintWriter对象
	         out.flush();//清空缓存
	         out.println("<script>");//输出script标签
	         out.println("alert(\"恭喜您，注册成功！请重新登录！\");");//js语句：输出alert语句
	         out.println("window.location=\"login.jsp\";");
	         out.println("</script>");//输出script结尾标签
//			request.getRequestDispatcher("login.jsp").forward(request,
//					response);
		} else {
			request.getRequestDispatcher("main.jsp").forward(request,
					response);
			System.out.println("添加失败!!");
		}

	}
	}

